package cn.wolfcode.trip.app.web.controller;


import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    //新增接口
    @Autowired
    private IRegionService          iRegionService;
    //新增接口,保存点评
    @Autowired
    private IStrategyCommentService iStrategyCommentService;
    //新增接口,前端页面显示标签以及大家都在看
    @Autowired
    private ITravelService          iTravelService;
    @Autowired
    private ICollectService         collectService;
    //保存自定义标签
    @Autowired
    private ITagService iTagService;


    @GetMapping("")
    public Object selectCommend(StrategyQueryObject qo){
        return strategyService.selectCommend(qo);
    }


    @GetMapping("/{id}")
    public Object selectByStrategyId(@PathVariable Long id){
        return strategyService.selectByStrategyId(id);
    }

    //通过strategy的id得到大家都在看的信息
    @GetMapping("/{strategyId}/travels")
    public Object selectTravelsByStrategyId(@PathVariable("strategyId")Long strategyId) {
        List<Travel> list = iTravelService.selectTravelsByStrategyId(strategyId);
        return list;
    }


    @PostMapping("/{userId}")
    public Object addCollect(CollectQueryObject qo){
        JSONResult json = new JSONResult();
        try {
            collectService.addStrategyCollect(qo);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @DeleteMapping("/{userId}")
    public Object removeCollect(CollectQueryObject qo){
        JSONResult json = new JSONResult();
        try {
            collectService.removeStrategyCollect(qo);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @GetMapping("/collectCount")
    public Object countCollect(Long strategyId){
        Integer num = collectService.countStrategyCollect(strategyId);
        return num;
    }

    @GetMapping("/{userId}/{strategyId}")
    public Object isCollect(@PathVariable Long userId,@PathVariable Long strategyId){
        Integer count = collectService.strategyIsCollect(userId,strategyId);
        return count!=0;
    }

    @GetMapping("/strategyCollect")
    public Object selectCollectByUserId(Long userId){
        return collectService.selectStrategyCollectByUserId(userId);
    }

    //个人评论中提交评论内容
    @PostMapping("/comments")
    public Object submitPersonalInfo(StrategyComment comment) throws Exception {
        JSONResult result = new JSONResult();
            try {
                //获取当前用户
                Long userId = UserContext.getCurrentUser().getId();
                //保存系统已经存在的标签和自定义标签
                //遍历自定义标签
                String[] myTags = comment.getMyTags();
                for (String myTag : myTags) {
                    iTagService.insertMyTags(myTag);
                    //查出保存自定义标签的id
                    Long num = iTagService.selectByMyTagsName(myTag);
                    //保存评论跟自定义标签之间的关系
                    iTagService.insertRelation(num, comment.getStrategy().getId());
                }
                //保存系统标签跟评论关系
                Long[] tagIds = comment.getTagIds();
                for (Long tagId : tagIds) {
                    iTagService.insertRelation(tagId,comment.getStrategy().getId());
                }
                comment.setUser(UserContext.getCurrentUser());
                comment.setCreateTime(new Date());
                comment.setCommendTime(new Date());
                //comment.set
                //将保存评论内容
                iStrategyCommentService.saveOrUpdate(comment);

                Long strategyId = comment.getId();

                //查出当前页面的strategyId,为前端跳转页面传递数据

                result.setData(strategyId);
            } catch (Exception e) {
                e.printStackTrace();
                result.setMsg("网络异常,保存失败");
            }
        return result;
    }

    @PutMapping("/{strategyId}")
    public Object updateViewCount(@PathVariable Long strategyId){

        strategyService.updateViewCount(strategyId);
        return null;
    }
}
